package com.js.common.core;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.context.annotation.Scope;

import javax.annotation.Resource;
import java.util.Map;

@Data
@Slf4j
@Scope("prototype")
public class BotCache {
    /**
     * 缓存跳过
     */
    private boolean skip = false;
    /**
     * 事件缓存前缀
     */
    private String cacheKeyPrefix;
    @Resource
    private CaffeineCache eventCache;

    public void setCache(String method, Map map){
        getEventCache().put(getCacheKeyPrefix() + method, map);
        setSkip(true);
    }

    public boolean containCache(String method){
        return getEventCache().get(getCacheKeyPrefix() + method) != null;
    }

    public Map<String, Object> getCache(String method){
        return (Map) getEventCache().get(getCacheKeyPrefix() + method).get();
    }

    public void removeCache(String method){
        getEventCache().evict(getCacheKeyPrefix() + method);
    }
}
